//
//  YoucApi.m
//  YoucApi
//
//  Created by Liam on 12-10-3.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "YoucApi.h"
#import "APIUtil.h"
#import "SingletonDefine.h"
#import "ApiTaskFactory.h"
#import "YoucApiTask.h"
#import "ApiTaskManager.h"

#import "YAReqGameList.h"
#import "YAReqSearchGames.h"
#import "YAReqGameServerList.h"
#import "YAReqUserRegister.h"
#import "YAReqUserUpdateInfo.h"
#import "YAReqUserGetAttention.h"
#import "YAReqUserAttention.h"
#import "YAReqPutDeviceToken.h"
#import "YAReqSetDND.h"

@interface YoucApi()

@end


@implementation YoucApi

@synthesize serverUrl;

OBJC_IMPL_SINGLETON(YoucApi)

- (id)init
{
	self = [super init];

	return self;
}

- (void)cancelTask:(id)task
{
	if (task && [task isKindOfClass:[ApiTaskBase class]])
	{
		[(ApiTaskBase*)task cancel];
	}
}

- (id)addTask:(id)task toManager:(Class)mgrClass
{
	if (task)
	{
		if (![NSThread isMainThread])
		{
			[[mgrClass instance] performSelectorOnMainThread:@selector(addOperation:) withObject:task waitUntilDone:NO];
		}
		else
		{
			[[mgrClass instance] performSelector:@selector(addOperation:) withObject:task];
		}
	}

	return task;
}

- (id)getTask:(TaskType)type params:(NSDictionary*)params context:(id)context delegate:(id)delegate
{
	ApiTaskBase * task = [ApiTaskFactory getApiTask:type params:params context:context delegate:delegate];
	return [self addTask:task toManager:[ApiTaskManager class]];
}

- (NSDictionary *)paramsWithSession:(YAReqWithSession *)reqWithSession otherParams:(NSDictionary *)otherParams
{
	NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
								   reqWithSession.userId, KEY_UID,
								   reqWithSession.session, KEY_SESSION,
								   nil];
	
	if (otherParams)
	{
		[params addEntriesFromDictionary:otherParams];
	}
	
	return params;
}

- (id)getGameList:(YAReqGameList *)reqGameList context:(id)context delegate:(id<YADelegateGame>)delegate
{
	NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
							[NSNumber numberWithInt:reqGameList.start], KEY_START,
							[NSNumber numberWithInt:reqGameList.limit], KEY_LIMIT,
							reqGameList.userId, KEY_UID,
							nil];
	return [self getTask:TaskGameGetGameList params:params context:context delegate:delegate];
}

- (id)searchGames:(YAReqSearchGames *)reqSearchGames context:(id)context delegate:(id<YADelegateGame>)delegate
{
	NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
								   reqSearchGames.searchKey, KEY_SEARCHKEY,
								   reqSearchGames.userId, KEY_UID,
								   nil];
	
	return [self getTask:TaskSearchGames params:params context:context delegate:delegate];
}

- (id)getGameServerList:(YAReqGameServerList *)reqGameServerList context:(id)context delegate:(id<YADelegateGame>)delegate
{
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            reqGameServerList.gameId, KEY_GAME,
							[NSNumber numberWithInt:reqGameServerList.start], KEY_START,
							[NSNumber numberWithInt:reqGameServerList.limit], KEY_LIMIT,
							nil];
	return [self getTask:TaskGameGetGameServerList params:params context:context delegate:delegate];
}

- (id)getRecommendList:(id)context delegate:(id<YADelegateGame>)delegate
{
    return [self getTask:TaskGameGetRecommendList params:nil context:context delegate:delegate];
}


- (id)getAttentionList:(YAReqUserGetAttention *)reqUserGetAttention context:(id)context delegate:(id<YADelegateGame>)delegate
{
	NSDictionary* params = [self paramsWithSession:reqUserGetAttention otherParams:nil];

	return [self getTask:TaskUserGetAttention params:params context:context delegate:delegate];
}

- (id)userRegister:(YAReqUserRegister *)reqUserRegister context:(id)context delegate:(id<YADelegateUser>)delegate
{
	NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            reqUserRegister.userAccount, KEY_USERACCOUNT,
							reqUserRegister.password, KEY_PASSWORD,
							reqUserRegister.rePassword, KEY_REPASSWORD,
							reqUserRegister.nickname, KEY_NICKNAME,
							nil];
	return [self getTask:TaskUserRegister params:params context:context delegate:delegate];
}

- (id)userLogin:(YAReqUserLogin *)reqUserLogin context:(id)context delegate:(id<YADelegateUser>)delegate
{
	NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            reqUserLogin.userAccount, KEY_USERACCOUNT,
							reqUserLogin.password, KEY_PASSWORD,
							nil];
	return [self getTask:TaskUserLogin params:params context:context delegate:delegate];
}

- (id)userKeepSession:(YAReqWithSession *)reqUserSession context:(id)context delegate:(id<YADelegateUser>)delegate
{
	NSDictionary* params = [self paramsWithSession:reqUserSession otherParams:nil];
	
	return [self getTask:TaskUserKeepSession params:params context:context delegate:delegate];
}

- (id)userGetInfo:(YAReqWithSession *)reqUserSession context:(id)context delegate:(id<YADelegateUser>)delegate
{
	NSDictionary* params = [self paramsWithSession:reqUserSession otherParams:nil];
	
	return [self getTask:TaskUserGetInfo params:params context:context delegate:delegate];
}

- (id)userUpdateInfo:(YAReqUserUpdateInfo *)reqUpdateUserInfo context:(id)context delegate:(id<YADelegateUser>)delegate
{
	NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            reqUpdateUserInfo.userInfo.nickname, KEY_NICKNAME,
							reqUpdateUserInfo.userInfo.sex, KEY_SEX,
							reqUpdateUserInfo.userInfo.sign, KEY_SIGN,
							reqUpdateUserInfo.userInfo.location, KEY_LOCATION,
							reqUpdateUserInfo.userInfo.photo, KEY_PHOTO,
							nil];
	
	params = [self paramsWithSession:reqUpdateUserInfo otherParams:params];
	
	return [self getTask:TaskUserUpdateInfo params:params context:context delegate:delegate];
}

- (id)userAddAttention:(YAReqUserAttention *)reqUserAttention context:(id)context delegate:(id<YADelegateUser>)delegate
{
	NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
							@"add", KEY_METHOD,
							reqUserAttention.gameId, KEY_ID,
							nil];

	params = [self paramsWithSession:reqUserAttention otherParams:params];
	
	return [self getTask:TaskUserAddAttention params:params context:context delegate:delegate];
}

- (id)userDeleteAttention:(YAReqUserAttention *)reqUserAttention context:(id)context delegate:(id<YADelegateUser>)delegate
{
	NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
							@"delete", KEY_METHOD,
							reqUserAttention.gameId, KEY_ID,
							nil];
	
	params = [self paramsWithSession:reqUserAttention otherParams:params];
	
	return [self getTask:TaskUserDeleteAttention params:params context:context delegate:delegate];
}

- (id)userPutDeviceToken:(YAReqPutDeviceToken *)reqPutDeviceToken
{
	NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
							reqPutDeviceToken.deviceToken, KEY_DEVICETOKEN,
							nil];
	
	params = [self paramsWithSession:reqPutDeviceToken otherParams:params];
	
	return [self getTask:TaskPutDeviceToken params:params context:nil delegate:nil];
}

- (id)userSetDND:(YAReqSetDND *)reqSetDND
{
	NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
							[NSNumber numberWithInt:reqSetDND.dndState], KEY_ISDND,
							[NSNumber numberWithInt:reqSetDND.dndType], KEY_DNDTYPE,
							nil];
	
	params = [self paramsWithSession:reqSetDND otherParams:params];

	return [self getTask:TaskSetDND params:params context:nil delegate:nil];
}

- (id)checkVersion:(NSString *)version context:(id)context delegate:(id<YADelegateClient>)delegate
{
	NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            version, KEY_APPVERSION,
							nil];
	return [self getTask:TaskCheckVersion params:params context:context delegate:delegate];
}

@end
